% This function computes, for the model with free entry, 
% the same outcomes as the GEobjects functions, and adds:

% - Equilibrium mass of firms (Nimplied)

% Note: unlike in GEobjects_FE_pre.m, this function does not assume fe is such
% that N=1. Instead it takes a given fe and finds the equilibrium N

% See Model with Free Entry Appendix

function [Nimplied,SP,SP2,P,RX,M,Iw, sN, sM,Ptot,LN] = GEobjects_FE(w,SPguess,sD, sX, phi,fc,fx,fe, param)

g = param.gamma;
eps = param.eps;
sg = param.sg;
eta = param.eta;
beta = param.beta;
qD = param.qD;
theta = param.theta;
vartheta = param.vartheta;
aN = param.aN;
AN = param.AN;
zeta = param.zeta;
varrho = param.varrho;
pM = param.pM;
T = param.T;
FXM = param.FXM; FX = param.FX; FM = param.FM;

pMw = pM / w;

% Constants:

k1 =  qD^(g*(sg-1)) * (1-g)^((1-g)*(sg-1)) * g^(g*(sg-1)) * g * (sg-1)^sg/sg^sg * eta;
k2 = qD^(g*(sg-1)) * (sg-1)^(sg-1)/sg^sg * (1-g)^((1-g)*(sg-1))* g^(g*(sg-1));
k3 = k2 * (theta-1)/theta;
k5 = (sg / (sg-1))^(1-sg) * (1-g)^((1-g)*(sg-1)) * (g*beta^(eps/(eps-1))*qD)^(g*(sg-1)); % kappa1
k6 = mean(k1*(sD.^(-1)-1).^(1/(eps-1)/eta) .* fc + k2 .* ((sD<1).*(sX==0)) * FM + k3 * (sX./(1-sX)).^(theta/(theta-1)) .*fx + k2 * ((sX>0).*(sD==1)) * FX + k2 * ((sX>0).*(sD<1)) * FXM ); % \varPsi
k7 = mean(phi.^(sg-1) .* (sX./(1-sX)) .*sD.^(-g*(sg-1)/(eps-1))); % \Upsilon
k8 = mean(phi.^(sg-1) .* (1./(1-sX)) .*sD.^(1-g*(sg-1)/(eps-1))); % \ Gamma, see Theory 9
Lambda = ((sg/ (sg-1))^(1/(1-g)) * (1-g)^(-1) * (g*beta^(eps/(eps-1))*qD)^(-g/(1-g)))^((sg-1)*(1-g)) *  (mean(phi.^(sg-1).*sD.^(-g*(sg-1)/(eps-1))))^(-1);% \Lambda; new term in free entry model

% Obtain N from free entry condition given fe:
Nimplied = (1/sg + k5* k7 * Lambda /sg - Lambda * k6) / fe * SPguess;

% Price indeces and expenditure shares:
P = Nimplied.^(-1/((sg-1)*(1-g))) * (sg/ (sg-1))^(1/(1-g)) * (1-g)^(-1) * (g*beta^(eps/(eps-1))*qD)^(-g/(1-g)) * (mean(phi.^(sg-1).*sD.^(-g*(sg-1)/(eps-1))))^(1/((1-sg)*(1-g))); % price index of locally-produced manufacturing bundle measured in domestic labor (P/w)
Pw_power = 1/Nimplied  * Lambda; % for use below
PM = (varrho^zeta * pMw^(1-zeta) + (1-varrho)^zeta * P^(1-zeta)).^(1/(1-zeta)); % price index of manufacturing bundle measured in domestic labor (PM/w) 
pNw = 1/AN; % price of non-tradable good measured in domestic labor (pN/w)
xx = PM/pNw;  % relative price of manufacturing bundle to non-tradable good
sN = aN^vartheta ./ (aN^vartheta + (1-aN)^vartheta *xx.^(1-vartheta)); % expenditure share on non-tradable
sM = varrho^zeta ./ (varrho^zeta + (1-varrho)^zeta *(P/pMw).^(1-zeta))  ; % expenditure share on imported final good
aT = (1-sN)* (1- sM); % share of expenditure on locally-produced manufacturing bundle (alpha wiggle in paper's notation)

% Obtain S/w associated with consumer budget constraint:
Sw = (param.L + param.T ) * aT / ( 1 - g * (sg-1)/sg * k5 * k8 * Nimplied * Pw_power); % fixed point in S, with entry costs in BC
% Obtain S/w associated with labor market clearing:
Sw2 = ((1-sN)* param.L - sN*param.T) * ( (1 +  (sg-1) * (1-g)) * ( 1/sg+ k5 * k7 * Pw_power / sg * Nimplied)).^(-1); % new LMK

SX = g* (sg-1)/sg * k5* P^((sg-1)*(1-g)) * k8 * Sw*Nimplied; % expenditure in local inputs by local firms measured in domestic labor (SX/w)
RX = k5  * P^((sg-1)*(1-g)) * Sw *k7*Nimplied; % aggregate exports measured in domestic labor (RX/w)
M = g * (sg-1)/sg * (Sw + RX) - SX; % imports of inputs measured in domestic labor (M/w)
Iw = param.L + T; % consumer income measured in domestic labor (I/w); note: with free entry net profits are zero
Ptot = (aN^vartheta .* pNw.^(1-vartheta) + (1-aN)^vartheta *PM.^(1-vartheta)).^(1/(1-vartheta)) ; % overall consumer price index (CES price index of the non-tradable price and the price index of the manufacturing bundle) measured in domestic labor
LN =  sN/aT * ( 1 - (sg-1)/sg * g *k5 *k8 * P^((sg-1)*(1-g)) )*Sw*Nimplied; % labor in non-tradable
P = P*w; SP = Sw*w; SP2 = Sw2*w; Ptot = Ptot *w;
end